package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @Author AzureSky_X
 * @Date 2020/11/16 17:30
 * @Version 1.0
 */
public class UserDaoImpl implements UserDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public User selectUserByUserName(String username) {
        //实际开发中绝对不允许使用*
        String sql="select * from tb_user where username =?";
        User query = null;
        try {
            query = qr.query(DbUtils.getConnection(), sql, new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
    //插入用户
    @Override
    public int insertUserByUser(User user) {
        String sql="insert into tb_user (username,password,email,gender,role,active_code,status) values (?,?,?,?,?,?,?)";
        int update = 0;
        try {
            update = qr.update(DbUtils.getConnection(), sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(), user.getRole(),user.getActive_code(),user.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }
    //更新激活码
    @Override
    public int updateStatusByActiveCode(String activeCode) {
        String sql="update tb_user set status=1 where active_code=?";
        int update = 0;
        try {
            update = qr.update(DbUtils.getConnection(), sql,activeCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;


    }
}
